package com.hydl.power.controller;

import com.github.pagehelper.PageInfo;
import com.hydl.power.mapper.UserFeedbackMapper;
import com.hydl.power.pojo.*;
import com.hydl.power.service.*;
import com.hydl.power.utils.FtpUtil;
import com.hydl.power.utils.Resp;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.text.ParseException;
import java.util.List;
import java.util.Map;

@Api(value = "AppController",tags = "App客户端接口")
@RequestMapping("/appUser")
@RestController
@CrossOrigin
public class AppUserController {

      @Autowired
      private AppUserService appUserService;

      @Autowired
      private AppService appService;

      @Autowired
      private InspectionplanService inspectionplanService;

      @Autowired
      private UserFeedbackMapper userFeedbackMapper;

      @Autowired
      private SystemUpdateContentService systemUpdateContentService;

      @Autowired
       private IncomingCabinetService incomingCabinetService;

      @Autowired
      private TemperatureDeviceService temperatureDeviceService;

      @Autowired
      private PowerDataService powerDataService;


      @ApiOperation("首页电量历史数据")
      @PostMapping("/getPowerDataByMonth")
      @ApiImplicitParams({
              @ApiImplicitParam(paramType = "query", name = "startTime", value = "开始时间2021-03-01",defaultValue = "2020-03-01" ,dataType = "String"),
              @ApiImplicitParam(paramType = "query", name = "endTime", value = "结束时间",defaultValue = "2021-03-26",dataType = "String"),
              @ApiImplicitParam(paramType = "query", name = "token", value = "token",defaultValue = "KXGC666666",dataType = "String"),

      })
      public Resp getPowerDataByMonth(String startTime,String endTime,String token) throws ParseException {
        List<List<PowerConsume>> lists = powerDataService.getPowerDataByMonth(startTime,endTime,token);
        return Resp.success(lists);
      }



      @PostMapping("getTemperatureDevicePowerData")
      @ApiOperation(value = "线缆测温设备历史数据", produces = "application/json")
      @ApiImplicitParams({
              @ApiImplicitParam(paramType = "query", name = "serialNumber", value = "设备sn",defaultValue = "525153485355485157485357_1", dataType = "String"),
              @ApiImplicitParam(paramType = "query", name = "time", value = "时间(2021-04-16)",defaultValue = "2021-04-16", dataType = "String"),
      })
      public Resp getTemperatureDevicePowerData(String serialNumber,String time) {
        List<TemperatureData> list=temperatureDeviceService.getTemperatureDevicePowerData(serialNumber,time);
        return Resp.success(list);
      }

      @PostMapping("getPowerData")
      @ApiOperation(value = "多功能表设备历史数据", produces = "application/json")
      @ApiImplicitParams({
              @ApiImplicitParam(paramType = "query", name = "serialNumber", value = "设备sn",defaultValue = "20200710753_1_1", dataType = "String"),
              @ApiImplicitParam(paramType = "query", name = "time", value = "时间(2021-04-22)",defaultValue = "2021-04-16", dataType = "String"),
      })
      public Resp getPowerData(String serialNumber,String time) {
        List<Powerdata> list=powerDataService.getPowerData(serialNumber,time);
        return Resp.success(list);
      }




  @PostMapping("getPower")
      @ApiOperation(value = "用电量", produces = "application/json")
      @ApiImplicitParams({
        @ApiImplicitParam(paramType = "query", name = "token", value = "token",defaultValue = "56149022", dataType = "String"),
      })
      public Resp getPower(String  token) throws ParseException {
        Map map=appUserService.electricQuantity(token);
        return Resp.success(map);
      }

    @PostMapping("queryProjectTransformer")
    @ApiOperation(value = "根据token查询项目变压器", produces = "application/json")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "token", value = "token",defaultValue = "KXGC666666", dataType = "String"),
    })
    public Resp queryProjectTransformer(String  token)  {
          List<Transformer> list = appUserService.queryDeviceTransformer(token);
        if(list==null){
            return Resp.success("未查询到数据",list,null);
        }
        if(list.size() == 0){
            return Resp.success("未查询到数据",list,null);
        }
        return Resp.success(list);
    }

  @PostMapping("queryMaxinloadRate")
  @ApiOperation(value = "负荷", produces = "application/json")
  @ApiImplicitParams({
    @ApiImplicitParam(paramType = "query", name = "powerRoomId", value = "配电房id",defaultValue = "56149022", dataType = "String"),
  })
  public Resp queryMaxinloadRate(String  powerRoomId){
    List map=appUserService.queryMaxinloadRate(powerRoomId);
    return Resp.success(map);
  }

  @PostMapping("queryProjectOrder")
  @ApiOperation(value = "报警列表", produces = "application/json")
  @ApiImplicitParams({
    @ApiImplicitParam(paramType = "query", name = "pageNum", value = "页码", required = true, dataType = "String"),
    @ApiImplicitParam(paramType = "query", name = "pageSize", value = "显示个数", required = true, dataType = "String"),
    @ApiImplicitParam(paramType = "query", name = "token", value = "token",defaultValue = "hhhhhhhhh", dataType = "String"),
  })
  public Resp queryProjectOrder(Integer pageNum,Integer pageSize,String  token){
    PageInfo<OrderDelivery> list = appUserService.queryProjectOrder(pageNum,pageSize,token);
    if(list == null) {
      return Resp.success("未查询到数据",null,null);
    }
    if(list.getSize() == 0 ){
      return Resp.success("未查询到数据",null,null);
    }
    return Resp.success(list);
  }

    @PostMapping("queryCityProject")
    @ApiOperation(value = "城市区域配电房", produces = "application/json")
    @ApiImplicitParams({
      @ApiImplicitParam(paramType = "query", name = "token", value = "token",defaultValue = "ZZGJ123JD", dataType = "String"),
    })
    public Resp queryCityProject(String  token){
      List list = appUserService.queryCityProject(token);
      if(list==null){
        return Resp.success("未查询到数据",null,null);
      }
      if(list.size() == 0){
        return Resp.success("未查询到数据",null,null);
      }
      return Resp.success(list);
    }

  @PostMapping("queryAppUserInspectionplan")
  @ApiOperation(value = "客户巡查计划列表", produces = "application/json")
  @ApiImplicitParams({
    @ApiImplicitParam(paramType = "query", name = "pageNum", value = "页码", required = true, dataType = "String"),
    @ApiImplicitParam(paramType = "query", name = "pageSize", value = "显示个数", required = true, dataType = "String"),
    @ApiImplicitParam(paramType = "query", name = "token", value = "token",defaultValue = "hhhhhhhhh", dataType = "String"),
    @ApiImplicitParam(paramType = "query", name = "creatTime", value = "creatTime",defaultValue = "2020-12", dataType = "String")
  })
  public Resp<PageInfo<Inspectionplan>> queryAppUserInspectionplan(Integer pageNum, Integer pageSize, String token, String creatTime){
    PageInfo<Inspectionplan> list = appUserService.queryAppUserInspectionplan(pageNum,pageSize,token,creatTime);
    System.out.println(list);
    if(list.getSize() == 0){
      return Resp.success("未查询到数据",null,null);
    }
    return Resp.success(list);
  }

  @PostMapping("queryOrderNumberSum")
  @ApiOperation(value = "待处理", produces = "application/json")
  @ApiImplicitParams({
    @ApiImplicitParam(paramType = "query", name = "token", value = "token",defaultValue = "ZZGJ123JD", dataType = "String"),
  })
  public Resp queryOrderNumberSum(String  token){
    int count = appUserService.queryProjectOrderNumberSum(token);
    return Resp.success(count);
  }

    @PostMapping("queryMaintainInspectionplan")
    @ApiOperation(value = "运维巡查计划列表", produces = "application/json")
    @ApiImplicitParams({
      @ApiImplicitParam(paramType = "query", name = "pageNum", value = "页码", required = true, dataType = "String"),
      @ApiImplicitParam(paramType = "query", name = "pageSize", value = "显示个数", required = true, dataType = "String"),
      @ApiImplicitParam(paramType = "query", name = "token", value = "token",defaultValue = "hhhhhhhhh", dataType = "String"),
      @ApiImplicitParam(paramType = "query", name = "creatTime", value = "creatTime",defaultValue = "2020-12", dataType = "String")
    })
    public Resp<PageInfo<Inspectionplan>> queryAppUserInspectionplans(Integer pageNum, Integer pageSize, String token,String creatTime){
      PageInfo<Inspectionplan> list = appUserService.queryAppUserInspectionplans( pageNum,pageSize,token,creatTime);
      System.out.println(list);
      if(list.getSize() == 0){
        return Resp.success("未查询到数据",null,null);
      }
      return Resp.success(list);
    }

    @PostMapping("queryoOffTheStocks")
    @ApiOperation(value = "按照时间条件查询已完成巡查工单", produces = "application/json")
    @ApiImplicitParams({
      @ApiImplicitParam(paramType = "query", name = "pageNum", value = "页码", required = true, dataType = "String"),
      @ApiImplicitParam(paramType = "query", name = "pageSize", value = "显示个数", required = true, dataType = "String"),
      @ApiImplicitParam(paramType = "query", name = "token", value = "token",defaultValue = "hhhhhhhhh", dataType = "String"),
      @ApiImplicitParam(paramType = "query", name = "createTime", value = "createTime",defaultValue = "2020-12", dataType = "String"),
      @ApiImplicitParam(paramType = "query", name = "dataType", value = "dataType",defaultValue = "", dataType = "String")
    })
    public Resp<PageInfo<InspectionOrder>> queryoOffTheStocks(Integer pageNum, Integer pageSize,String token,String createTime,String dataType){
      PageInfo<InspectionOrder> list = inspectionplanService.queryoOffTheStocks(pageNum,pageSize,token,createTime,dataType);
      if(list.getSize() == 0){
        return Resp.success("未查询到数据",null,null);
      }
      return Resp.success(list);
    }

    @PostMapping("getUserPowerRoom")
    @ApiOperation(value = "查询配电房列表", produces = "application/json")
    @ApiImplicitParams({
      @ApiImplicitParam(paramType = "query", name = "token", value = "token",defaultValue = "ZZGJ123JD", dataType = "String"),
    })
    public Resp getUserPowerRoom(String  token){
        List<PowerRoom> list = appUserService.getUserPowerRoom(token);
      if(list==null){
        return Resp.success("未查询到数据",list,null);
      }
      if(list.size() == 0){
        return Resp.success("未查询到数据",list,null);
      }
      return Resp.success(list);
    }

  @PostMapping("addUserOrder")
  @ApiOperation(value = "用户添加报警", produces = "application/json")
  @ApiImplicitParams({
    @ApiImplicitParam(paramType = "query", name = "token", value = "token",defaultValue = "", dataType = "String"),
    @ApiImplicitParam(paramType = "query", name = "fault", value = "报警内容",defaultValue = "", dataType = "String"),
    @ApiImplicitParam(paramType = "query", name = "deviceName", value = "设备名称",defaultValue = "", dataType = "String"),
    @ApiImplicitParam(paramType = "query", name = "rank", value = "rank",defaultValue = "", dataType = "Integer"),
    @ApiImplicitParam(paramType = "query", name = "faultTypes", value = "faultTypes",defaultValue = "", dataType = "Integer"),
    @ApiImplicitParam(paramType = "query", name = "roomName", value = "roomName",defaultValue = "", dataType = "String"),
    @ApiImplicitParam(paramType = "query", name = "roomId", value = "roomId",defaultValue = "", dataType = "String"),
  })
  public Resp addUserOrder(String  token, String fault, String deviceName, Integer rank, Integer faultTypes, String roomName, String roomId,String imageUrl) {
    int i = appService.addUserOrder( token,fault,deviceName,rank,faultTypes,roomName,roomId,imageUrl);
      if(i == 1 ){
          return  Resp.success("添加成功");
      }
      return Resp.fail("添加失败");
  }

  @PostMapping("addUserFeed")
  @ApiOperation(value = "添加反馈", produces = "application/json")
  @ApiImplicitParams({
          @ApiImplicitParam(paramType = "query", name = "text", value = "反馈内容",defaultValue = "", dataType = "String"),
          @ApiImplicitParam(paramType = "query", name = "userName", value = "用户名称",defaultValue = "", dataType = "String"),
  })
  public Resp addUserFeed(String text, String userName){
    int i = userFeedbackMapper.addUserFeed(text,userName);
    if(i==1){
      return Resp.success("添加成功");
    }
    return Resp.fail("添加失败");
  }


    @PostMapping("querySystemUpdate")
    @ApiOperation(value = "查看是否有新版本", produces = "application/json")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "identify", value = "0：pc 1：客户端 2运维端",defaultValue = "", dataType = "Integer"),
    })
    public Resp queryIsSystemUpdate(Integer identify){
      SystemUpdateContent systemUpdateContent=systemUpdateContentService.querySystemUpdate(identify);
      if(systemUpdateContent==null){
        return Resp.result("400","未查询到数据");
      }
      return Resp.success(systemUpdateContent);
    }

  @PostMapping("queryAllSystemUpdate")
  @ApiOperation(value = "查看系统更新记录", produces = "application/json")
  @ApiImplicitParams({
          @ApiImplicitParam(paramType = "query", name = "identify", value = "用户名称",defaultValue = "", dataType = "Integer"),
  })
  public Resp querySystemUpdate(Integer identify){
    List<SystemUpdateContent> list=systemUpdateContentService.queryAllSystemUpdate(identify);
    if(list==null){
      return Resp.result("400","未查询到数据");
    }
    return Resp.success(list);
  }

  @PostMapping("addSystemMessage")
  @ApiOperation(value = "添加系统更新信息", produces = "application/json")
  @ApiImplicitParams({
          @ApiImplicitParam(paramType = "query", name = "title", value = "更新标题",defaultValue = "", dataType = "String"),
          @ApiImplicitParam(paramType = "query", name = "content", value = "更新内容",defaultValue = "", dataType = "String"),
          @ApiImplicitParam(paramType = "query", name = "versionNumberName", value = "用户名称",defaultValue = "", dataType = "String"),
          @ApiImplicitParam(paramType = "query", name = "versionNumber", value = "code",defaultValue = "", dataType = "String"),
          @ApiImplicitParam(paramType = "query", name = "identify", value = "标识位",defaultValue = "", dataType = "Integer"),
  })
  public Resp addSystemMessage(String title, String content, String versionNumberName, String versionNumber, Integer identify){
        int i = systemUpdateContentService.addSystemMessage(title,content,versionNumberName,versionNumber,identify);
    if(i == 1){
      return Resp.result("200","添加成功");
    }
    return Resp.result("400","添加失败");
  }

  @PostMapping("queryeTheSameDayPower")
  @ApiOperation(value = "查询每日电量", produces = "application/json")
  @ApiImplicitParams({
          @ApiImplicitParam(paramType = "query", name = "token", value = "token",defaultValue = "HLGJ85231", dataType = "String"),
          @ApiImplicitParam(paramType = "query", name = "strartTime", value = "查询时间",defaultValue = "2020-03-06", dataType = "String"),
  })
  public Resp queryeTheSameDayPower(String token, String strartTime){
    List<IncomingCabinet> list = incomingCabinetService.queryeTheSameDayPower(token,strartTime);
    if(list == null){
      return Resp.result("200","未查询到数据");
    }
    return Resp.success(list);
  }
}
